import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";

// 创建路由规则
const routes: any = [
  {
    path: "/",
    name: "login",
    component: () => import("../views/LoginView.vue"),
  },
  // 忘记密码
  {
    path: "/recover",
    name: "recover",
    component: () => import("../views/ForgotView.vue"),
  },
  // 注册
  {
    path: "/register",
    name: "register",
    component: () => import("../views/RegisterView.vue"),
  },
  {
    path: "/home",
    name: "home",
    component: HomeView,
    children: [
      // {
      //   path: 'userlist',
      //   component: () => import('../views/UserListView.vue')
      // },
      // {
      //   path: 'log',
      //   component: () => import('../views/LogView.vue')
      // },
    ],
  },
  {
    path: "/about",
    name: "about",
    // route level code-splitting
    // this generates a separate chunk (About.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import("../views/AboutView.vue"),
  },
  {
    path: "/line",
    name: "line",
    component: () => import("../views/DemoLineView.vue"),
  },
];

// 创建router实例
const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 检查用户是否认证，例如检查token
  // let token = window.sessionStorage.getItem('token');
  // to.name
  if (false) {
    next({ path: "/login", query: { redirect: to.fullPath } });
  } else {
    next();
  }
});

export default router;
